package cloud.xuxiaowei.core.config;

import cloud.xuxiaowei.core.annotation.ControllerAdviceAnnotation;
import cloud.xuxiaowei.core.utils.MethodParameterUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.core.MethodParameter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter;

import java.lang.reflect.Type;

/**
 * 默认 {@link RequestBodyAdvice} 适配器，保持原始数据，不进行处理，仅用于演示
 *
 * @author xuxiaowei
 * @since 0.0.1
 */
@Slf4j
@Component
@ConditionalOnClass(RequestBodyAdviceAdapter.class)
public class DefaultRequestBodyAdviceAdapter extends RequestBodyAdviceAdapter {

	@Override
	public boolean supports(MethodParameter methodParameter, Type targetType,
			Class<? extends HttpMessageConverter<?>> converterType) {
		boolean hassed = methodParameter.hasMethodAnnotation(ControllerAdviceAnnotation.class);

		String methodName = MethodParameterUtils.getMethodName(methodParameter);

		log.debug("方法: {} 是否使用 {}：{}", methodName, this.getClass().getName(), hassed);

		return hassed;
	}

}
